package com.ruoyi.utils;

import org.geotools.geometry.jts.WKTReader2;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;

import java.util.ArrayList;
import java.util.List;

/**
 * 图形创建工具
 *
 * @author liuliya
 * @date 2023-07-19
 */
public class GeometryUtil {

    private GeometryUtil() {
    }

    private static GeometryFactory geometryFactory = new GeometryFactory();

    /**
     * 创建点
     *
     * @param coordinate
     * @return
     */
    public static Point createPoint(Coordinate coordinate) {
        return geometryFactory.createPoint(coordinate);
    }

    /**
     * 创建线
     *
     * @param coordinates
     * @return
     */
    public static LineString createLine(Coordinate[] coordinates) {
        return geometryFactory.createLineString(coordinates);
    }

    /**
     * 创建线性环
     *
     * @param coordinates
     * @return
     */
    public static LinearRing createLinearRing(Coordinate[] coordinates) {
        Coordinate[] destCoordinates = new Coordinate[coordinates.length + 1];
        System.arraycopy(coordinates, 0, destCoordinates, 0, coordinates.length);
        destCoordinates[coordinates.length] = coordinates[0];
        return geometryFactory.createLinearRing(destCoordinates);
    }


    /**
     * 创建面
     *
     * @param coordinates
     * @return
     */
    public static Polygon createPolygon(Coordinate[] coordinates) {
        Coordinate[] destCoordinates = new Coordinate[coordinates.length + 1];
        System.arraycopy(coordinates, 0, destCoordinates, 0, coordinates.length);
        destCoordinates[coordinates.length] = coordinates[0];
        return geometryFactory.createPolygon(destCoordinates);
    }

    /**
     * 创建带洞的面
     *
     * @param outerCoords
     * @param holeCoords
     * @return
     */
    public static Polygon createPolygonWithHole(Coordinate[] outerCoords, Coordinate[] holeCoords) {
        LinearRing outerBorder = createLinearRing(outerCoords);
        LinearRing innerBorder = createLinearRing(holeCoords);
        return geometryFactory.createPolygon(outerBorder, new LinearRing[]{innerBorder});
    }

    /**
     * 创建带多个洞的面
     *
     * @param outer
     * @param holes
     * @return
     */
    public static Polygon createPolygonWithHoles(LinearRing outer, LinearRing[] holes) {
        return geometryFactory.createPolygon(outer, holes);
    }


    /**
     * 创建多面
     *
     * @param polygons
     * @return
     */
    public static MultiPolygon createMultiPolygon(Polygon[] polygons) {
        return geometryFactory.createMultiPolygon(polygons);
    }


    /**
     * 创建多面
     *
     * @param polygons
     * @return
     */
    public static MultiPolygon createMultiPolygon(List<String> wktList){
        List<Geometry> geometryList = new ArrayList<>();
        wktList.forEach(wkt -> {
            //读取坐标
            WKTReader2 wktReader = new WKTReader2();
            try {
                Geometry read = wktReader.read(wkt);
                geometryList.add(read);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
        //联合所有的坐标
        Geometry geometry = geometryList.get(0);
        for (int i = 1; i < geometryList.size(); i++) {
            geometry = geometry.union(geometryList.get(i));
        }
        return (MultiPolygon)geometry;
    }
}
